package com.coding.mapper;


import com.coding.entity.Message;
import com.coding.entity.vo.MsgVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author Cynicism
 * @version 1.0
 * @project ChatDemo
 * @description
 * @date 2024/6/22 10:34:52
 */
@Mapper
public interface MessageMapper {
    @Insert("INSERT INTO message (sender_id, receiver_id, content,created_at) VALUES (#{senderId}, #{receiverId}, #{content}, #{createdAt})")
    Integer insert(MsgVO message);

    @Select("SELECT COUNT(*) FROM message WHERE (sender_id = #{senderId} AND receiver_id = #{receiverId}) OR (sender_id = #{receiverId} AND receiver_id = #{senderId})")
    int getMessageCountBetweenUsers(@Param("senderId") Long senderId, @Param("receiverId") Long receiverId);


    @Select("SELECT * FROM message WHERE (sender_id = #{senderId} AND receiver_id = #{receiverId}) OR (sender_id = #{receiverId} AND receiver_id = #{senderId}) ORDER BY created_at DESC ")
    List<Message> getMessagesBetweenUsers(@Param("senderId") Long senderId, @Param("receiverId") Long receiverId);


    @Select("SELECT content FROM message WHERE (sender_id = #{senderId} AND receiver_id = #{receiverId}) OR (sender_id = #{receiverId} AND receiver_id = #{senderId}) ORDER BY created_at DESC LIMIT 1")
    String getLastMessage(@Param("senderId") Long senderId, @Param("receiverId") Long receiverId);



}
